package com.zhugang.week03;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program algorithms
 * @description: myStack
 * @author: chanzhugang
 * @create: 2022/06/04 15:28
 */
public class MyStack {

    Queue<Integer> queue;

    /**
     * 225.用队列实现栈
     * <p>
     * push：倒腾队列；pop、peek直接取
     */
    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        int n = queue.size();
        queue.offer(x);
        for (int i = 0; i < n; i++) {
            // 出队后重新入队
            queue.offer(queue.poll());
        }
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}